/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJFrame.java
 *
 * Created on May 3, 2011, 11:21:05 PM
 */
package GUI;

import com.mh.ObjectClasses.Route;
import com.zx.Logic.*;
import com.zx.ObjectClasses.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author VcLi
 */
public class Main extends javax.swing.JFrame {
    //initialize an object of ts to set up all the maps

    private TrainManager tm = new TrainManager();
    private Map<String, TrainService> trainServiceMap = null;
    private Map<String, Station> stnMap = null;
    private Station stn = null;
    private TrainService ts = null;
    private Collection c = null;
    private Iterator itr = null;
    private DefaultListModel listModel = null;
    private Route newRoute = new Route();
    private LinkedList<Route> llRoutes = new LinkedList<Route>();
    private Station stnFrom = new Station();
    private Station stnTo = new Station();
    //This is to determine which tab is running so that the SharedListSelectionL-
    //istener can be reused to other tab
    //By default, it's on the first tab which index is 0
    static int currentTab = 0;

    /** Creates new form NewJFrame */
    public Main() {
        initComponents();
        tabPanel.addChangeListener(new tabbedPanelChangeListener());
        // set up the tabbed panel to display staff
        this.displayTrainService();
        this.displayStn();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        tabPanel = new javax.swing.JTabbedPane();
        allTsTab = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        tsList = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        tblTrainService = new javax.swing.JTable();
        searchPnlRt1 = new javax.swing.JPanel();
        txtSearchTs = new javax.swing.JTextField();
        tsSearch = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        allStnTab = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        stnList = new javax.swing.JList();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblStnInfo = new javax.swing.JTable();
        searchPnlRt2 = new javax.swing.JPanel();
        txtSearchStn = new javax.swing.JTextField();
        stnSearch = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        routeSearchTab = new javax.swing.JPanel();
        searchPnlRt3 = new javax.swing.JPanel();
        txtFrom = new javax.swing.JTextField();
        routeSearch = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtTo = new javax.swing.JTextField();
        jScrollPane5 = new javax.swing.JScrollPane();
        fromList = new javax.swing.JList();
        jScrollPane6 = new javax.swing.JScrollPane();
        toList = new javax.swing.JList();
        msgOnRoute = new javax.swing.JLabel();
        RtMainPnl = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane8 = new javax.swing.JScrollPane();
        jScrollPane7 = new javax.swing.JScrollPane();
        txtBestRoute = new javax.swing.JTextPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("MainFrame"); // NOI18N
        setResizable(false);

        mainPanel.setName("ParentPanel"); // NOI18N

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 5, Short.MAX_VALUE)
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 443, Short.MAX_VALUE)
        );

        tabPanel.setName("tabPanel"); // NOI18N

        jScrollPane4.setViewportView(tsList);

        tblTrainService.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Station No", "Route"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane3.setViewportView(tblTrainService);
        tblTrainService.getColumnModel().getColumn(0).setMaxWidth(70);
        tblTrainService.getColumnModel().getColumn(1).setResizable(false);

        tsSearch.setFont(new java.awt.Font("Tahoma", 0, 14));
        tsSearch.setText("Go!");
        tsSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tsSearchActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel2.setText("Search:");

        javax.swing.GroupLayout searchPnlRt1Layout = new javax.swing.GroupLayout(searchPnlRt1);
        searchPnlRt1.setLayout(searchPnlRt1Layout);
        searchPnlRt1Layout.setHorizontalGroup(
            searchPnlRt1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(searchPnlRt1Layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtSearchTs, javax.swing.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tsSearch)
                .addContainerGap())
        );
        searchPnlRt1Layout.setVerticalGroup(
            searchPnlRt1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(searchPnlRt1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(searchPnlRt1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtSearchTs, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tsSearch))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        jLabel7.setText("Please Select a Train Service:");

        javax.swing.GroupLayout allTsTabLayout = new javax.swing.GroupLayout(allTsTab);
        allTsTab.setLayout(allTsTabLayout);
        allTsTabLayout.setHorizontalGroup(
            allTsTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(allTsTabLayout.createSequentialGroup()
                .addGap(140, 140, 140)
                .addComponent(searchPnlRt1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(194, Short.MAX_VALUE))
            .addGroup(allTsTabLayout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(allTsTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(allTsTabLayout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, allTsTabLayout.createSequentialGroup()
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 420, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(22, 22, 22))))
        );
        allTsTabLayout.setVerticalGroup(
            allTsTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, allTsTabLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(searchPnlRt1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(allTsTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(38, 38, 38))
        );

        tabPanel.addTab(" Train Service", allTsTab);

        stnList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane2.setViewportView(stnList);

        tblStnInfo.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Station No", "Train Service Operating in selected Station"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblStnInfo);
        tblStnInfo.getColumnModel().getColumn(0).setMaxWidth(70);
        tblStnInfo.getColumnModel().getColumn(1).setResizable(false);

        stnSearch.setFont(new java.awt.Font("Tahoma", 0, 14));
        stnSearch.setText("Go!");
        stnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stnSearchActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel3.setText("Search:");

        javax.swing.GroupLayout searchPnlRt2Layout = new javax.swing.GroupLayout(searchPnlRt2);
        searchPnlRt2.setLayout(searchPnlRt2Layout);
        searchPnlRt2Layout.setHorizontalGroup(
            searchPnlRt2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(searchPnlRt2Layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtSearchStn, javax.swing.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(stnSearch)
                .addContainerGap())
        );
        searchPnlRt2Layout.setVerticalGroup(
            searchPnlRt2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(searchPnlRt2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(searchPnlRt2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtSearchStn, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(stnSearch))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        jLabel8.setText("Please Select a Station :");

        javax.swing.GroupLayout allStnTabLayout = new javax.swing.GroupLayout(allStnTab);
        allStnTab.setLayout(allStnTabLayout);
        allStnTabLayout.setHorizontalGroup(
            allStnTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(allStnTabLayout.createSequentialGroup()
                .addGap(141, 141, 141)
                .addComponent(searchPnlRt2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(193, Short.MAX_VALUE))
            .addGroup(allStnTabLayout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(allStnTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(allStnTabLayout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, allStnTabLayout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 420, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(22, 22, 22))))
        );
        allStnTabLayout.setVerticalGroup(
            allStnTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, allStnTabLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(searchPnlRt2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(allStnTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(38, 38, 38))
        );

        tabPanel.addTab("Station Information", allStnTab);

        searchPnlRt3.setBorder(javax.swing.BorderFactory.createTitledBorder("Search"));

        txtFrom.setEditable(false);
        txtFrom.setEnabled(false);

        routeSearch.setFont(new java.awt.Font("Tahoma", 0, 14));
        routeSearch.setText("Go!");
        routeSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                routeSearchActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel4.setText("From :");

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel5.setText("To:");

        txtTo.setEditable(false);
        txtTo.setEnabled(false);

        fromList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane5.setViewportView(fromList);

        toList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane6.setViewportView(toList);

        msgOnRoute.setForeground(new java.awt.Color(255, 0, 51));
        msgOnRoute.setText("This is the warning String");

        javax.swing.GroupLayout searchPnlRt3Layout = new javax.swing.GroupLayout(searchPnlRt3);
        searchPnlRt3.setLayout(searchPnlRt3Layout);
        searchPnlRt3Layout.setHorizontalGroup(
            searchPnlRt3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, searchPnlRt3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(searchPnlRt3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(msgOnRoute, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 403, Short.MAX_VALUE)
                    .addGroup(searchPnlRt3Layout.createSequentialGroup()
                        .addGroup(searchPnlRt3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
                            .addComponent(txtFrom, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(searchPnlRt3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
                            .addComponent(txtTo, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(63, 63, 63)
                .addComponent(routeSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(54, 54, 54))
        );

        searchPnlRt3Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jScrollPane5, jScrollPane6});

        searchPnlRt3Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {txtFrom, txtTo});

        searchPnlRt3Layout.setVerticalGroup(
            searchPnlRt3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(searchPnlRt3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(searchPnlRt3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, searchPnlRt3Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(49, 49, 49)
                        .addComponent(routeSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(86, 86, 86))
                    .addGroup(searchPnlRt3Layout.createSequentialGroup()
                        .addGroup(searchPnlRt3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, searchPnlRt3Layout.createSequentialGroup()
                                .addGroup(searchPnlRt3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(txtFrom, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel5))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, searchPnlRt3Layout.createSequentialGroup()
                                .addComponent(txtTo, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jScrollPane6, 0, 0, Short.MAX_VALUE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(msgOnRoute, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        searchPnlRt3Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {routeSearch, txtFrom, txtTo});

        RtMainPnl.setBorder(javax.swing.BorderFactory.createTitledBorder("Result"));

        jLabel1.setText("Best Route:");

        jScrollPane7.setViewportView(txtBestRoute);

        jScrollPane8.setViewportView(jScrollPane7);

        javax.swing.GroupLayout RtMainPnlLayout = new javax.swing.GroupLayout(RtMainPnl);
        RtMainPnl.setLayout(RtMainPnlLayout);
        RtMainPnlLayout.setHorizontalGroup(
            RtMainPnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(RtMainPnlLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(RtMainPnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane8, javax.swing.GroupLayout.DEFAULT_SIZE, 712, Short.MAX_VALUE)
                    .addComponent(jLabel1))
                .addContainerGap())
        );
        RtMainPnlLayout.setVerticalGroup(
            RtMainPnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(RtMainPnlLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane8, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout routeSearchTabLayout = new javax.swing.GroupLayout(routeSearchTab);
        routeSearchTab.setLayout(routeSearchTabLayout);
        routeSearchTabLayout.setHorizontalGroup(
            routeSearchTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, routeSearchTabLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(routeSearchTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(RtMainPnl, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(searchPnlRt3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        routeSearchTabLayout.setVerticalGroup(
            routeSearchTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(routeSearchTabLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(searchPnlRt3, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(RtMainPnl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        tabPanel.addTab("Search for Route", routeSearchTab);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tabPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 769, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(32, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(tabPanel, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                    .addComponent(mainPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-806)/2, (screenSize.height-524)/2, 806, 524);
    }// </editor-fold>//GEN-END:initComponents

    private void tsSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tsSearchActionPerformed
        this.searchTsStn();
    }//GEN-LAST:event_tsSearchActionPerformed

    private void stnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stnSearchActionPerformed
        this.searchTsStn();
    }//GEN-LAST:event_stnSearchActionPerformed

    private void routeSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_routeSearchActionPerformed
        //this.searchRoute();
        String strFrom = txtFrom.getText();
        String strTo = txtTo.getText();
        stnFrom = stnMap.get(strFrom);
        stnTo = stnMap.get(strTo);
        this.searchStation(stnFrom, stnTo);
        ArrayList<Route> sortedRoutes = new ArrayList<Route>(llRoutes);
        Collections.sort(sortedRoutes);

        this.txtBestRoute.setText(getRouteInformation(sortedRoutes));
        System.out.println(getRouteInformation(sortedRoutes));
    }//GEN-LAST:event_routeSearchActionPerformed

    private void displayTrainService() {
        //int counter = 0;
        listModel = new DefaultListModel();
        //call the method to retrieve the map
        trainServiceMap = tm.getTrainServiceMap();

        //get all the keys of the map, put it into an array and sort them
        List sortedTsMap = new ArrayList(trainServiceMap.keySet());
        Collections.sort(sortedTsMap);

        for (Object temp : sortedTsMap) {
            listModel.addElement(temp.toString());
        }

        //set up the tsList
        tsList.setModel(listModel);
        tsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        tsList.addListSelectionListener(new SharedListSelectionHandler());
    }

    private void displayStn() {
        listModel = new DefaultListModel();
        //call the method to retrieve the map
        stnMap = tm.getStnMap();

        //get all the keys of the map, put it into an array and sort them
        List sortedStnMap = new ArrayList(stnMap.keySet());
        Collections.sort(sortedStnMap);

        for (Object temp : sortedStnMap) {
            listModel.addElement(temp.toString());
        }

        stnList.setModel(listModel);
        stnList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //stnList.setSelectedIndex(0);

        fromList.setModel(listModel);
        fromList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //fromList.setSelectedIndex(0);

        toList.setModel(listModel);
        toList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        //toList.setSelectedIndex(0);

        stnList.addListSelectionListener(new SharedListSelectionHandler());
        fromList.addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                //if the selection is still continuing, stop the process
                if (e.getValueIsAdjusting()) {
                    return;
                }

                JList theList = (JList) e.getSource();

                String tsKeyValue = theList.getSelectedValue().toString();
                txtFrom.setText(tsKeyValue);
                System.out.println("--" + tsKeyValue + "--");
            }
        });
        toList.addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                //if the selection is still continuing, stop the process
                if (e.getValueIsAdjusting()) {
                    return;
                }

                JList theList = (JList) e.getSource();

                String tsKeyValue = theList.getSelectedValue().toString();
                txtTo.setText(tsKeyValue);
                System.out.println("--" + tsKeyValue + "--");
            }
        });
    }

    private String blurSearchWithKey(JTextField source, Map hashMap) {
        String mapKey = "";
        String inputStr = "";
        List<String> keyValueList = null;

        inputStr = source.getText().trim();
        keyValueList = new ArrayList<String>(hashMap.keySet());
        for (String key : keyValueList) {
            //able to do the blur search but not case sensitive for blur search
            //1.ignore the case but must input the whole string
            //2.(?i) turns on the insentivity,reference:
            //http://www.regular-expressions.info/modifiers.html
            if (key.equalsIgnoreCase(inputStr) || key.matches("(?i).*" + inputStr + ".*")) {
                try {
                    mapKey = key;
                    return mapKey;
                } catch (NullPointerException e) {
                    System.out.println("Not Found");
                }
            }
        }
        return "";
    }

    private void searchTsStn() {
        String mapKey = "";
        switch (currentTab) {
            //to search for certain trainservice and display the stations
            case 0:
                //call the generic methods and pass in the values
                mapKey = this.blurSearchWithKey(txtSearchTs, trainServiceMap);
                if (!mapKey.equals("")) {
                    ts = trainServiceMap.get(mapKey);
                    selectAndDisplay(ts, tblTrainService);
                }
                break;
            case 1:
                //call the generic methods and pass in the values
                mapKey = this.blurSearchWithKey(txtSearchStn, stnMap);
                if (!mapKey.equals("")) {
                    stn = stnMap.get(mapKey);
                    selectAndDisplay(stn, tblStnInfo);
                }
                break;
            case 2:
                break;
        }

        //In future, can further develop this to feedback to user using JOptionPane
        if (!mapKey.equals("")) {
            System.out.println("Found");
        } else {
            System.out.println("Not Found");
        }
    }

    private void searchRoute() {
        String fromStr = txtFrom.getText().trim();
        String toStr = txtTo.getText().trim();
        Station departure = null;
        Station destination = null;



        if (fromStr.equals("") || toStr.equals("")) {
            msgOnRoute.setText("Please complete your starting station and destination");
            return;
        } else {
            String mapKey1 = this.blurSearchWithKey(txtFrom, stnMap);
            String mapKey2 = this.blurSearchWithKey(txtTo, stnMap);

            departure = stnMap.get(mapKey1);
            destination = stnMap.get(mapKey2);

            //direct Train Service when dept and dest are on the sam service
            //working
            this.directTrainService(departure, destination);
            //situtation whereby they need to transfer
            
            //not woking, will throw nullpointer.
            //this.transferTrainService(departure, destination);


        }

    }
    //Working if the service are on the same service
    private void directTrainService(Station departure, Station destination) {
        ArrayList<TrainService> listDeparture = departure.getOperatingTsInStn();
        ArrayList<TrainService> listDestination = destination.getOperatingTsInStn();
        //to count how many stops 
        int counter = 0;
        String stationsPassed = "";

        //situtaion whereby they are on the same line, no transfer needed
        outer:
        if (listDeparture.size() == 1 && listDestination.size() == 1) {

            if (listDeparture.get(0).getName().equals(listDestination.get(0).getName())) {
                TrainService tsTemp = trainServiceMap.get(listDeparture.get(0).getName());
                ArrayList<Station> temp = tsTemp.getStnList();
                for (Station stnTemp : temp) {
                    if (departure.getName().equals(stnTemp.getName())) {
                        counter = 1;
                        stationsPassed += departure.getName() + "-";
                    }
                    if (!stationsPassed.equals("")) {
                        stationsPassed += stnTemp.getName() + "-";
                    }
                    if (destination.getName().equals(stnTemp.getName())) {
                        txtBestRoute.setText("Please take " + tsTemp.getName()
                                + "\tThere are " + counter + "stations in between");
                        
                        //lblBestRoute.setText(stationsPassed);
                        break outer;
                    }
                    counter++;
                }
            }
        }
    }
    
    //Not working for now
    private void transferTrainService(Station departure, Station destination) {
        ArrayList<TrainService> listDeparture = departure.getOperatingTsInStn();
        ArrayList<TrainService> listDestination = destination.getOperatingTsInStn();

        Station interchange = null;
        int counter2 = 0;

        if (!listDeparture.get(0).getName().equals(listDestination.get(0).getName())) {
            interchange = this.findInterchange(listDeparture, listDestination);
            if (interchange != null) {
                TrainService tsLine1 = trainServiceMap.get(interchange.getName());
                TrainService tsLine2 = trainServiceMap.get(interchange.getName());
                ArrayList<Station> tempLine1 = tsLine1.getStnList();
                ArrayList<Station> tempLine2 = tsLine2.getStnList();

                outer:
                for (Station stnTemp : tempLine1) {
                    if (departure.getName().equals(stnTemp.getName())) {
                        counter2 = 1;
                        //stationsPassed += departure.getName() + "-";
                    }
//                                    if (!stationsPassed.equals("")) {
//                                        //stationsPassed += stnTemp.getName() + "-";
//                                    }
                    if (interchange.getName().equals(stnTemp.getName())) {
                        break outer;
                    }
                    counter2++;
                }

                outer2:
                for (Station stnTemp : tempLine2) {
                    if (interchange.getName().equals(stnTemp.getName())) {
                        counter2 = 1;
                        //stationsPassed += departure.getName() + "-";
                    }
//                                    if (!stationsPassed.equals("")) {
//                                        stationsPassed += stnTemp.getName() + "-";
//                                    }
                    if (destination.getName().equals(stnTemp.getName())) {
//                        txtAltRoute.setText(""
//                                + "Please take Train Service " + tsLine1.getName() + ""
//                                + " \nTransfer at " + interchange.getName() + ""
//                                + " to Train Service " + tsLine2.getName() + ""
//                                + " \nTotal stops:" + counter2 + "");

                        break outer2;
                    }
                    counter2++;
                }
            }
        }
    }

    // Haven't Tested, may not be working
    //pass in the arrayList contains in Statin object,if the size >2, then means the 
    //station cross at least 2 TrainService
    private Station findInterchange(ArrayList<TrainService> listDeparture,
            ArrayList<TrainService> listDestination) {

        int counter2 = 1;
        Station interchange = null;
        boolean matchDeparture = false;
        boolean matchDestination = false;
        //boolean interchangeFound = false;

        while (counter2 < listDeparture.size() && counter2 < listDestination.size()) {
            c = stnMap.values();
            itr = c.iterator();
            while (itr.hasNext()) {
                stn = (Station) itr.next();
                if (stn.isInterchange()) {
                    for (TrainService tsOne : stn.getOperatingTsInStn()) {
                        for (TrainService tsTwo : listDeparture) {
                            if (tsOne.getName().equals(tsTwo.getName())) {
                                matchDeparture = true;
                            }
                        }

                        for (TrainService tsThree : listDestination) {
                            if (tsOne.getName().equals(tsThree.getName())) {
                                matchDestination = true;
                            }
                        }

                        if (matchDeparture && matchDestination) {
                            interchange = stn;
                        }
                    }
                }
            }
            counter2++;
        }
        return interchange;
    }

    public void searchStation(Station startStation, Station endStation) {
        llRoutes = new LinkedList<Route>();
        if (hasCommonTrainServices(startStation, endStation)) {
            directTrainService1(startStation, endStation);
        } else {
            transferTrainService1(startStation, endStation);
        }
    }

    public void directTrainService1(Station startStation, Station endStation) {
        ArrayList<TrainService> alTs = new ArrayList<TrainService>();
        alTs = getCommonTrainServices(startStation, endStation);

        for (TrainService ts : alTs) {
            formRoute(ts, startStation, endStation);
        }
    }

    public void transferTrainService1(Station startStation, Station endStation) {
        Set<Station> sEndInterchanges = new HashSet();
        sEndInterchanges = findInterchanges(endStation);

        Set<Station> sStartInterchanges = new HashSet();
        sStartInterchanges = findInterchanges(startStation);

        for (Station endInterchange : sEndInterchanges) {
            for (Station startInterchange : sStartInterchanges) {
                if (endInterchange.getName().equals(startInterchange.getName())) {//require one transfer
                    //formRoute with start, end and interchange
                    formRoute(startInterchange, startStation, endStation);
                }
            }
        }
    }

    public boolean hasCommonTrainServices(Station startStation, Station endStation) {
        ArrayList<TrainService> alCommonTS = new ArrayList<TrainService>();
        for (TrainService startTS : startStation.getOperatingTsInStn()) {
            if (endStation.getOperatingTsInStn().contains(startTS)) {
                return true;
            }
        }
        return false;
    }

    public ArrayList<TrainService> getCommonTrainServices(Station startStation, Station endStation) {
        Set<TrainService> sTS = new HashSet();
        for (TrainService startTs : startStation.getOperatingTsInStn()) {
            if (endStation.getOperatingTsInStn().contains(startTs)) {
                sTS.add(startTs);
            }
        }
        ArrayList<TrainService> alTs = new ArrayList<TrainService>();
        alTs.addAll(sTS);
        return alTs;
    }

    public boolean hasCommonInterchanges(ArrayList<TrainService> startTs, ArrayList<TrainService> endTs) {
        ArrayList<Station> startInterchanges = new ArrayList<Station>();
        ArrayList<Station> endInterchanges = new ArrayList<Station>();

        startInterchanges = getInterchanges(startTs);
        endInterchanges = getInterchanges(endTs);

        for (Station station : startInterchanges) {
            if (endInterchanges.contains(station)) {
                return true;
            }
        }
        return false;
    }

    public ArrayList<Station> getCommonInterchanges(ArrayList<TrainService> startTs, ArrayList<TrainService> endTs) {
        Set<Station> sS = new HashSet();
        ArrayList<Station> alStartInterchanges = new ArrayList<Station>();
        ArrayList<Station> alEndInterchanges = new ArrayList<Station>();

        alStartInterchanges = getInterchanges(startTs);
        alEndInterchanges = getInterchanges(endTs);

        for (Station startStn : alStartInterchanges) {
            if (alEndInterchanges.contains(startStn)) {
                sS.add(startStn);
            }
        }

        ArrayList<Station> alInterchanges = new ArrayList<Station>();
        alInterchanges.addAll(sS);

        return alInterchanges;
    }

    //get the interchanges with the TrainService arraylist, return ArrayList<Station>
    public ArrayList<Station> getInterchanges(ArrayList<TrainService> alTS) {
        ArrayList<Station> alInterchanges = new ArrayList<Station>();
        for (TrainService ts : alTS) {
            for (Station s : ts.getStnList()) {
                if (s.isInterchange()) {
                    alInterchanges.add(s);
                }
            }
        }
        return alInterchanges;
    }
    //find the interchange with the Station, return SET
    public Set<Station> findInterchanges(Station station) {
        Set<Station> sInterchanges = new HashSet();
        for (TrainService ts : station.getOperatingTsInStn()) {
            for (Station s : ts.getStnList()) {
                if (s.isInterchange()) {
                    sInterchanges.add(s);
                }
            }
        }
        return sInterchanges;
    }

    public void formRoute(TrainService ts, Station startStation, Station endStation) {//for direct train
        Route newRoute = new Route();
        newRoute.setStartStation(startStation);
        newRoute.setEndStation(endStation);
        newRoute = insertRoute(newRoute, startStation, endStation, ts);
        newRoute.setIsDirectTrainService(true);
        newRoute.setStartTrainService(ts);
        newRoute.setEndTrainService(ts);
        llRoutes.add(newRoute);
    }

    public void formRoute(Station interchange, Station startStation, Station endStation) {//form route with interchange in teh middle
        Route newRoute = new Route();
        System.out.println("Interchange name : " + interchange.getName());
        ArrayList<TrainService> startTs = new ArrayList<TrainService>();
        startTs = getCommonTrainServices(startStation, interchange);

        ArrayList<TrainService> endTs = new ArrayList<TrainService>();
        endTs = getCommonTrainServices(interchange, endStation);

        for (TrainService sTs : startTs) {
            newRoute = new Route();
            newRoute.setInterchange(interchange);
            newRoute.setStartStation(startStation);
            newRoute.setEndStation(endStation);

            newRoute = insertRoute(newRoute, startStation, interchange, sTs);
            newRoute.addStationIntoMiddle(newRoute.getRouteSize()-1, interchange);
            newRoute.setStartTrainService(sTs);
            for (TrainService eTs : endTs) {
                Route tempRoute = new Route();
                tempRoute = newRoute;
                tempRoute = insertRoute(tempRoute, interchange, endStation, eTs);
                newRoute.setEndTrainService(eTs);
                newRoute.setIsDirectTrainService(false);
                llRoutes.add(tempRoute);
            }
        }
    }

    public Route insertRoute(Route r, Station startStation, Station endStation, TrainService ts) {
        int intStart = 0;
        int intEnd = 0;
        boolean blnNextLoop = false;
        boolean blnInsertRoute = false;

        intStart = ts.getStationNumber(startStation);
        intEnd = ts.getStationNumber(endStation);

        if (intStart > intEnd) {
            ts.reverseStationList();
        }
        for (Station stn : ts.getStnList()) {
            blnNextLoop = startStation.getName().equals(stn.getName());
            if (blnNextLoop) {
                blnNextLoop = false;
                blnInsertRoute = true;
                continue;
            }
            if (blnInsertRoute) {
                if (!endStation.getName().equals(stn.getName())) {
                    r.addStationIntoMiddle(r.getRouteSize() - 1, stn);
                } else {
                    break;
                }
            }
        }
        return r;
    }

    public String getRouteInformation(ArrayList<Route> sortedALRoutes){
        String strBestRoute = "";
        Route bestRoute = sortedALRoutes.get(0);
        LinkedList<Station> llBestRouteStations = new LinkedList<Station>();
        llBestRouteStations = bestRoute.getStations();
        if(bestRoute.isDirectTrainService()){
            strBestRoute = "This service does not require transfer.\n";
            strBestRoute += "Number of stops : " + bestRoute.getRouteSize() + "\n\n";
            strBestRoute += "Train Service : " + bestRoute.getStartTrainService().getName();
            strBestRoute += "\n\nThe stations :";
            for(Station s : llBestRouteStations){
                strBestRoute += "\n" + s.getName() + "";
            }
        }else{
            strBestRoute = "This service requires 1 transfer.\n";
            strBestRoute += "Number of stops : " + bestRoute.getRouteSize() + "\n\n";
            strBestRoute += "Starting Train Service : " + bestRoute.getStartTrainService().getName() + "\n";
            strBestRoute += "Interchange : " + bestRoute.getInterchange().getName() + "\n";
            strBestRoute += "Ending Train Service : " + bestRoute.getEndTrainService().getName() + "\n";
            strBestRoute += "\n\nThe stations :";
            for(Station s : llBestRouteStations){
                strBestRoute += "\n" + s.getName() + "";
                if(s.getName().equals(bestRoute.getInterchange().getName())){
                    strBestRoute += "\t(Please transfer to " + bestRoute.getEndTrainService().getName() + ")";
                }
            }
        }

        return strBestRoute;
    }

    private void selectAndDisplay(Object o, JTable tblName) {
        //To remove all the elements left by the previous selection
        DefaultTableModel tableModel = (DefaultTableModel) tblName.getModel();
        tableModel.getDataVector().removeAllElements();

        ArrayList list = null;
        String[] rowData = new String[2];
        if (o instanceof TrainService) {
            //get the list stored in the trainservice object
            list = ((TrainService) o).getStnList();
            //Collections.sort(list, Station.stationNameComparator);
        } else if (o instanceof Station) {
            list = ((Station) o).getOperatingTsInStn();
            //use the customized Comparator the sort the name of the trainService
            //in ascending order.
            Collections.sort(list, TrainService.trainnameComparator);
        }

        int counter = 0;

        for (Object listItems : list) {

            //a little bit formatting of the text here
            String s = "";
            int stationNo = counter + 1;
            if (stationNo < 10) {
                s = "0" + stationNo;
            } else {
                s = "" + stationNo;
            }

            if (o instanceof TrainService) {
                //get the list stored in the trainservice object
                //set up the data to display on the table
                rowData[0] = s;
                rowData[1] = ((Station) listItems).getName();

            } else if (o instanceof Station) {

                //set up the data to display on the table
                rowData[0] = s;
                rowData[1] = ((TrainService) listItems).getName();
            }

            //set up the data to display on the table
            tableModel.insertRow(counter, rowData);
            counter++;
        }
    }

    class tabbedPanelChangeListener implements ChangeListener {

        // This method is called whenever the selected tab changes
        public void stateChanged(ChangeEvent e) {
            JTabbedPane pane = (JTabbedPane) e.getSource();
            // Get current tab
            int sel = pane.getSelectedIndex();
            //To change the current tab index
            Main.currentTab = sel;
            System.out.println(sel);
        }
    }

    class SharedListSelectionHandler implements ListSelectionListener {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            //if the selection is still continuing, stop the process
            if (e.getValueIsAdjusting()) {
                return;
            }

            JList theList = (JList) e.getSource();
            String tsKeyValue = theList.getSelectedValue().toString();
            System.out.println("--" + tsKeyValue + "--");

            // make use of the current tab to know what actions should be performed

            switch (Main.currentTab) {
                case 0:
                    if (!tsKeyValue.equals("")) {
                        System.out.println(tsKeyValue);
                        ts = trainServiceMap.get(tsKeyValue);
                        selectAndDisplay(ts, tblTrainService);
                    }
                    break;
                case 1:
                    if (!tsKeyValue.equals("")) {
                        System.out.println(tsKeyValue);
                        stn = stnMap.get(tsKeyValue);
                        selectAndDisplay(stn, tblStnInfo);
                    }
                    break;
                case 2:
                    break;
            }


        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Main().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel RtMainPnl;
    private javax.swing.JPanel allStnTab;
    private javax.swing.JPanel allTsTab;
    private javax.swing.JList fromList;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JScrollPane jScrollPane7;
    private javax.swing.JScrollPane jScrollPane8;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JLabel msgOnRoute;
    private javax.swing.JButton routeSearch;
    private javax.swing.JPanel routeSearchTab;
    private javax.swing.JPanel searchPnlRt1;
    private javax.swing.JPanel searchPnlRt2;
    private javax.swing.JPanel searchPnlRt3;
    private javax.swing.JList stnList;
    private javax.swing.JButton stnSearch;
    private javax.swing.JTabbedPane tabPanel;
    private javax.swing.JTable tblStnInfo;
    private javax.swing.JTable tblTrainService;
    private javax.swing.JList toList;
    private javax.swing.JList tsList;
    private javax.swing.JButton tsSearch;
    private javax.swing.JTextPane txtBestRoute;
    private javax.swing.JTextField txtFrom;
    private javax.swing.JTextField txtSearchStn;
    private javax.swing.JTextField txtSearchTs;
    private javax.swing.JTextField txtTo;
    // End of variables declaration//GEN-END:variables
}
